<?php
/*
 * маппер комметариев.
 */
class Model_CommentsMapper
{
    protected $_dbTable;
    
    public function __construct($dbTable = null)
    {
        if($dbTable === null){
            $this->setDbTable(new Model_DbTable_Comment());
        } else {
            $this->setDbTable($dbTable);
        }
    }
    
    public function setDbTable($dbTable)
    {
        $this->_dbTable = $dbTable;   
    }
    
    public function getDbTable()
    {
        return $this->_dbTable;
    }
    
    public function save($comment)
    {
        $data =  array (
            'id' =>$comment->id,
            'username'  => $comment->username,
            'email'     => $comment->email,
            'message'   => $comment->message,
            'post_id'   => $comment->post_id,
            'parent_id' => $comment->parent_id
        );
        if(!$data['parent_id']){
            $data['parent_id'] = null;
        }
        if($comment->id === null){
            $data['date'] = new Zend_Db_Expr('NOW()');
            $commentId = $this->_dbTable->insert($data);
            $comment->id = $commentId;
        } else {
            $this->_dbTable->update($data, array('id = ?' => (integer) $comment->id));
        }
        return $comment;
    }
    
    public function find($id)
    {
        $row = $this->_dbTable->find($id)->current();
        if($row){
            return new Model_Comment(
                array(
                  'id'        => $row->id,
                  'username'  => $row->username,
                  'email'     => $row->email,
                  'message'   => $row->message,
                  'post_id'   => $row->post_id,
                  'parent_id' => $row->parent_id,
                  'date'      => $row->date
                )
            );
        } else {
            return null;
        }
    }
    
    public function delete($comment)
    {
        $this->_dbTable->delete(array('id = ?' => (integer) $comment->id));
    }
    //получить комметарии
    public function getCommentsByPost($post)
    {
        $rowset = $this->_dbTable->fetchAll(array('post_id = ?' => (integer) $post->id));
        $comments = array();
        foreach($rowset as $row){
            $comments[$row->id] = new Model_Comment(
                array(
                  'id'        => $row->id,
                  'username'  => $row->username,
                  'email'     => $row->email,
                  'message'   => $row->message,
                  'post_id'   => $row->post_id,
                  'parent_id' => $row->parent_id,
                  'date'      => $row->date
                ));
        }
        return $comments;
    }
    // получить кол-во комметов в посте. 
    public function getCommentsCountByPost($post)
    {
        $select = $this->_dbTable->select()
            ->from($this->_dbTable, array('count'=>'count(0)'))
            ->where('post_id = ?' , (integer) $post->id);
        $row = $this->_dbTable->fetchRow($select);
        return $row->count;
    }
}